const merge = require('webpack-merge')
const common = require('./webpack.common')
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const TerserWebpackPlugin = require('terser-webpack-plugin')
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const path = require('path')

module.exports = merge(common, {
  mode: 'production',
  plugins: [
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, './public/'),
        to: path.resolve(__dirname, './dist'),
        ignore: [/\.html|\.ejs/]
      }
    ]),
    new HardSourceWebpackPlugin() //为模块提供中间缓存步骤。为了查看结果，您需要使用此插件运行webpack两次：第一次构建将花费正常的时间。第二次构建将显着加快（大概提升90%的构建速度）
  ],
  optimization: {
    minimizer: [
      new OptimizeCssAssetsWebpackPlugin({
        //css压缩
        assetNameRegExp: /\.css$/g,
        cssProcessor: require('cssnano'),
        cssProcessorOptions: {
          safe: true,
          discardComments: { removeAll: true }
        },
        canPrint: true
      }),
      new TerserWebpackPlugin({
        //js压缩
        cache: true,
        parallel: true
      })
    ]
  }
})
